<?php session_start();

$_SESSION = array();

require_once 'visibility.php';
require_once 'db_utility.php';

// connect to database from here
$conn = db_connect();

// generate a visibility type for user
$type = gen_visibility_type();
$ipaddress = $_SERVER['REMOTE_ADDR'];

// get assignmentId, workerId, hitId
$assignmentId = $_POST['assignmentId'];
$workerId = $_POST['workerId'];
$hitId = $_POST['hitId'];

$_SESSION['mturk']['assignmentId'] = $assignmentId;

$result = $conn->query("INSERT INTO experiments (visibility, hit_id, worker_id, assignment_id, ipaddress) 
		values ('$type', '$hitId', '$workerId', '$assignmentId','$ipaddress')");

if(!$result){
	echo 'no';
	$conn->close();
	exit;
}

// set all session variables
$_SESSION['user']['u_id'] = get_last_insert_id($conn);
$_SESSION['user']['u_visibility'] = $type;
$_SESSION['user']['u_opr_count'] = 0;
$_SESSION['stories']['onURL'] = array();
$_SESSION['stories']['onButton'] = array();

$conn->close();
echo 'success';
exit;

?>